package com.example.toutiao.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.toutiao.R;

public class SettingsActivity extends AppCompatActivity {

    private LinearLayout layoutAccountManage, layoutLogout;
    private LinearLayout layoutAddArticle, layoutAddProduct;
    private Switch switchAdmin;

    @Override
    @SuppressLint("MissingInflatedId")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        TextView tabBarTitle = findViewById(R.id.tabBarTitle);
        tabBarTitle.setText("设置");
        layoutAccountManage = findViewById(R.id.layout_account_manage);
        layoutLogout = findViewById(R.id.layout_logout);
        layoutAddArticle = findViewById(R.id.layout_add_article);
        layoutAddProduct = findViewById(R.id.layout_add_product);
        switchAdmin = findViewById(R.id.switch_admin);

        // 初始隐藏管理员功能
        layoutAddArticle.setVisibility(View.GONE);
        layoutAddProduct.setVisibility(View.GONE);

        // 跳转到账号管理页面
        layoutAccountManage.setOnClickListener(v -> {
            startActivity(new Intent(this, AccountManagementActivity.class));
        });
        layoutLogout.setOnClickListener(v -> {
            finishAffinity();
            startActivity(new Intent(this, LoginActivity.class));
        });

        // 管理员模式切换
        switchAdmin.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                layoutAddArticle.setVisibility(View.VISIBLE);
                layoutAddProduct.setVisibility(View.VISIBLE);
            } else {
                layoutAddArticle.setVisibility(View.GONE);
                layoutAddProduct.setVisibility(View.GONE);
            }
        });

        layoutAddArticle.setOnClickListener(v -> {
            startActivity(new Intent(this, AddArticleActivity.class));
        });

        layoutAddProduct.setOnClickListener(v -> {
            startActivity(new Intent(this, AddProductActivity.class));
        });
    }
}
